/**
  ETFDAQ Project
  \class TASIS3100
  \brief A wrapper class for SIS3100 controller
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2024-08-11
  \date 2024-08-11 last modified

  \copyright Copyright (c) 2024 IMP-CAS with LGPLv3 LICENSE
*/

#ifndef TASIS3100_h
#define TASIS3100_h

#include "sis1100_var.h"
#include "TAController.h"

class TASIS3100 : public TAController{
public:
  TASIS3100(const string &dev);
  virtual ~TASIS3100();

  virtual void Reset() override;
  ///--- processing interrupts ---///
  /// \param mask: irq levels(1~7,3=1<<3), 0xFE: all 1~7 irq levels are enabled
  virtual void EnableIRQ(int mask = 0xFE) override; ///< enable interrupt w.r.t. mask
  virtual void DisableIRQ() override; ///< disable all the irqs
  virtual void GetIRQ() const override; ///< fetch and show interrupt status
  virtual void PrintIRQStatus() override; ///< show the IRQ register

  ///--- the daq loop ---///
  virtual void DropDead() override; ///< set ch_out to forbit the trigger
  virtual void Resurrect() override; ///< clear the set ch_out

  /// generate a pulse to front-panel IO ///
  /// LEMO out, NIM Logic, -800mV, width=12.5ns, ch=1~3 ///
  void GeneratePulse_LEMO_OUT(int ch);
  void Set_LEMO_OUT(int ch);
  void Clear_LEMO_OUT(int ch);
  /// TTL or ECL, flat cable out, ch=1~4 ///
  void GeneratePulse_FLAT_OUT(int ch);
  void Set_FLAT_OUT(int ch);
  void Clear_FLAT_OUT(int ch);

  ///--- read and write the control registers ---///
  virtual void CtrlReadD32(int addr) override;
  virtual void CtrlWriteD32(int addr, uint32_t data) override; ///< val |= data

  ///--- read and write data via VME bus ---///
  virtual uint32_t A32D32Read(uint32_t addr) override;
  virtual uint16_t A32D16Read(uint32_t addr) override;
  virtual void A32D32Write(uint32_t addr, uint32_t data) override;
  virtual void A32D16Write(uint32_t addr, uint16_t data) override;
  /// \retval nof words read \param nreq: requested nof 32-bit words to be read
  virtual int A32BLT32Read(uint32_t addr, uint32_t *data, uint32_t nreq) override;
  /// \retval nof words read \param nreq: requested nof **32-bit** words to be read
  virtual int A32MBLT64Read(uint32_t addr, uint32_t *data, uint32_t nreq) override;

  /// register offsets ///
  static const int OFFSET_OPTINOUT = 0x80;
  static const int OFFSET_CTRL = 0x100;
  static const int OFFSET_IRQSC = 0x104; // sc: status/control

protected:
  sis1100_irq_ctl fIRQCTL_ON; ///< the default IRQ enable variable
  sis1100_irq_ctl fIRQCTL_OFF; ///< the default IRQ disable variable
};

#endif
